<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0">
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title>密码笔记本</title>
		<link rel="stylesheet" type="text/css" href="aui-20170109-v2.1/css/aui.css"/>
	</head>
	<body>
		<header class="aui-bar aui-bar-nav">
			<a class="aui-pull-left aui-btn" onclick="back()">
				<span class="aui-iconfont aui-icon-left"></span>
			</a>
			<div class="aui-title"></div>
			<a class="aui-pull-right aui-btn aui-btn-outlined">
				<i id="updata_icon" class="aui-iconfont aui-icon-pencil" onclick="updata_btn()"></i>
				<i id="save_icon" style="display: none" class="aui-iconfont aui-icon-correct" onclick="save_btn()"></i>
			</a>
			<a class="aui-pull-right aui-btn aui-btn-outlined" onclick="del()">
				<i class="aui-iconfont aui-icon-trash"></i>
			</a>
		</header>
		<div class="aui-content aui-margin-b-15">
			<ul class="aui-list aui-form-list">
<!--				<li class="aui-list-header">双击切换修改</li>-->
				<li class="aui-list-item">
					<div class="aui-list-item-inner" id="title_show">
						<div class="aui-list-item-label">
							标题
						</div>
						<div id="title" class="aui-list-item-input">
							QQ账号
						</div>
						<div class="aui-list-item-input" id="title_updata" style="display: none">
							<input type="text" placeholder="title">
						</div>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label">
							账号
						</div>
						<div id="account" class="aui-list-item-input">
							Bbullet
						</div>
						<div class="aui-list-item-input" id="account_updata" style="display: none">
							<input type="text" placeholder="account">
						</div>
						<div class="aui-btn aui-btn-info" onclick="copyToClipboard('account')">复制</div>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label">
							密码
						</div>
						<div id="password" class="aui-list-item-input">
							1093570153z2fdae
						</div>
						<div class="aui-list-item-input" style="display: none" id="password_updata">
							<input type="text" placeholder="password">
						</div>
						<div class="aui-btn aui-btn-info" onclick="copyToClipboard('password')">复制</div>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label">
							常用
						</div>
						<div class="aui-list-item-input">
							<input type="checkbox" class="aui-switch" id="stick">
						</div>
					</div>
				</li>
			</ul>
		</div>
	</body>
	<script src="js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="aui-20170109-v2.1/script/aui-dialog.js" type="text/javascript" charset="utf-8"></script>
	<script src="aui-20170109-v2.1/script/aui-toast.js" type="text/javascript" charset="utf-8"></script>
	<script src="aui-20170109-v2.1/script/api.js" type="text/javascript" charset="utf-8"></script>
	<script src="js/sha3.js"></script>
	<script src="js/aes.js"></script>
	<script src="js/md5.js"></script>
	<script src="js/cloud_sync.js"></script>
	<script type="text/javascript">
		var dialog = new auiDialog();
		var toast = new auiToast();
		var noteId = getQueryVariable("noteId");
		var data= JSON.parse(localStorage.getItem(noteId));
		$(function () {
			init();
		})
		//初始化数据
		function init() {
			$("#title_updata").find("input").val(data.title);
			$("#account_updata").find("input").val(data.account);
			$("#password_updata").find("input").val(data.password);
			$("#title").text(data.title);
			$("#account").text(data.account);
			$("#password").text(data.password);
			data.stick ? $("#stick")[0].checked=true : "";
		}
		$("#stick").change(function (o) {
			data.stick = $("#stick")[0].checked;
			localStorage.setItem(noteId, JSON.stringify(data));
			let pwnotes = JSON.parse(localStorage.getItem("PWnotes"));
			for (let i = 0; i < pwnotes.list.length; i++) {
				if (pwnotes.list[i].noteId == noteId) {
					pwnotes.list[i].stick = $("#stick")[0].checked;
				}
			}
			localStorage.setItem("PWnotes", JSON.stringify(pwnotes));
		});
		//url取参数
		function getQueryVariable(variable)
		{
			var query = window.location.search.substring(1);
			var vars = query.split("&");
			for (var i=0;i<vars.length;i++) {
				var pair = vars[i].split("=");
				if(pair[0] == variable){return pair[1];}
			}
			return(false);
		}
		//返回
		function back() {
			history.back();
		}
		//修改切换
		function updata_btn(){
			$("#save_icon").show();
			$("#title_updata").show();
			$("#account_updata").show();
			$("#password_updata").show();
			$("#updata_icon").hide();
			$("#title").hide();
			$("#account").hide();
			$("#password").hide();
			$(".aui-btn-info").hide();
		}
		//保存切换
		function save_btn() {
			var k = false;
			$("input").each(function () {
				if (k) return;
				let val = $.trim($(this).val());
				if (!val) {
					dialog.alert({
						title:"Error!",
						msg:"请勿留空！",
						buttons:["关闭"]
					})
					k = true;
				}
			});
			if (k) return;
			data = {
				title: $("#title_updata").find("input").val(),
				account: $("#account_updata").find("input").val(),
				password: $("#password_updata").find("input").val(),
				stick: $("#stick")[0].checked
			}
			localStorage.setItem(noteId, JSON.stringify(data));
			init();
			let pwnotes = JSON.parse(localStorage.getItem("PWnotes"));
			for (let i = 0; i < pwnotes.list.length; i++) {
				if (pwnotes.list[i].noteId == noteId) {
					pwnotes.list[i].stick = $("#stick")[0].checked;
					pwnotes.list[i].title = data.title;
				}
			}
			pwnotes.version = new Date().getTime();
			localStorage.setItem("PWnotes", JSON.stringify(pwnotes));
			// if (is_sync=="true") cloud_sync();

			$("#save_icon").hide();
			$("#title_updata").hide();
			$("#account_updata").hide();
			$("#password_updata").hide();
			$("#updata_icon").show();
			$("#title").show();
			$("#account").show();
			$("#password").show();
			$(".aui-btn-info").show();
		}
		//删除
		function del() {
			dialog.alert({
				title:"提示",
				msg:"确定删除？",
				buttons:["确定","取消"]
			},function (res) {
				if (res.buttonIndex == 1) {
					localStorage.removeItem(noteId);
					let pwnotes = JSON.parse(localStorage.getItem("PWnotes"));
					for (let i = 0; i < pwnotes.list.length; i++) {
						if (pwnotes.list[i].noteId == noteId) {
							pwnotes.list.splice(i, 1);
							break;
						}
					}
					pwnotes.version = new Date().getTime();
					localStorage.setItem("PWnotes", JSON.stringify(pwnotes));
					toast.success({
						title: '删除成功！',
						duration: 1000
					});
					// if (is_sync=="true") cloud_sync();
					setTimeout(function () {
						location.href = 'PWnotes.html';
					}, 1000);
				}
			})
		}
		//复制内容到粘贴板
		function copyToClipboard(elemId) {
			window.event? window.event.cancelBubble = true : e.stopPropagation();
		    var elem = document.getElementById(elemId);
		    var targetId = "_hiddenCopyText_";
		    var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
		    var origSelectionStart, origSelectionEnd;
		    if (isInput) {
		        // 复制选择内容
		        target = elem;
		        origSelectionStart = elem.selectionStart;
		        origSelectionEnd = elem.selectionEnd;
		    } else {
		        // 必须有一个临时的元素存储复制的内容
		        target = document.getElementById(targetId);
		        if (!target) {
		            var target = document.createElement("textarea");
		            target.style.position = "absolute";
		            target.style.left = "-9999px";
		            target.style.top = "0";
		            target.id = targetId;
		            document.body.appendChild(target);
		        }
		        // target.textContent = elem.textContent;
		        target.textContent = $.trim(elem.textContent);
		    }
		    // 选择内容
		    var currentFocus = document.activeElement;
		    target.focus();
		    target.setSelectionRange(0, target.value.length);
		    // 复制内容
		    var succeed;
		    try {
		        succeed = document.execCommand("copy");
		    } catch (e) {
		        succeed = false;
		    }
		    // 恢复焦点
		    if (currentFocus && typeof currentFocus.focus === "function") {
		        currentFocus.focus();
		    }
		    if (isInput) {
		        // 恢复之前的选择
		        elem.setSelectionRange(origSelectionStart, origSelectionEnd);
		    } else {
		        // 清除临时内容
		        target.textContent = "";
		    }
		    console.log("复制成功");
		    return succeed;
		}
	</script>
</html>
